<?php

/**
 * docente actions.
 *
 * @package    ibe
 * @subpackage docente
 * @author     Creatix
 */
class docenteActions extends sfActionsCreatix
{
  public function executeIndex(sfWebRequest $request)
  {
    return $this->forward('docente', 'list');
  }

	public function executeGrid()
	{
		//Recupero todas as informações necessárias para trabalhar com o grid
		$this->getRequestDataTable();
		$this->data					= DocenteTable::consultar($this->filter, $this->order);
		$this->total_data			= count($this->data);
		$this->data					= $this->paginadorDataTable($this->data, $this->page_num, parent::LIMIT_PAGE_DATA_TABLE);

		sfLoader::loadHelpers(array('Url'));

	    foreach($this->data as $key => $r)
	    {
	    					    	$actions = array(
				'edit'	 => "<a href='".url_for($this->getModuleName().'/edit?id_docente='.$r['id_docente'])."' class='icoEditar'></a>",
				'delete' => "<a href='".url_for($this->getModuleName().'/delete?id_docente='.$r['id_docente'])."'  class='icoExcluir delete'></a>",
			);
																																																																																																																																																																																																																			    	$this->data[$key]['act'] = implode(' ', $actions);
    	}

		$this->setJSONDataTable($this->data, $this->colunas, $this->total_data);
		return sfView::NONE;
	}


    public function executeList()
    {
        $this->form = new DocenteForm();
    	$this->colunas	= array(
    		      		$this->form['id_docente']->renderLabel() => "id_docente",
			      		$this->form['nm_docente']->renderLabel() => "nm_docente",
			      		$this->form['nu_matricula']->renderLabel() => "nu_matricula",
			      		$this->form['dt_nascimento']->renderLabel() => "dt_nascimento",
			      		$this->form['nu_cpf']->renderLabel() => "nu_cpf",
			      		$this->form['ds_cargo']->renderLabel() => "ds_cargo",
			      		$this->form['st_docente']->renderLabel() => "st_docente",
						"Ações"	=> "act"
		);

        $this->filter = $this->_getFilter();

        $this->setTemplate('index');
    }

    public function executeFilter()
	{
	    $filter = new Filtro(
			$this->getRequestParameter(sfActionsCreatix::FILTER_SESSION_USER_ATTRIBUTE)
		);

		$filter->setCulture($this->getUser()->getCulture());
		$this->getUser()->setAttribute('pg_num',1,$this->getModuleName());

		$this->getUser()->setAttribute(
			sfActionsCreatix::FILTER_SESSION_USER_ATTRIBUTE,
			serialize($filter),
			$this->getModuleName()
		);

		return $this->forward($this->getModuleName(), '/list');
	}

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new DocenteForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new DocenteForm();

    $this->processForm($request, $this->form);

    $this->forward($this->getModuleName(), 'new');
  }

  public function validateCreate()
  {
  	return true;
  }

  public function handleErrorCreate()
  {
    return $this->forward($this->getModuleName(), 'new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_docente'))), sprintf('Object docente does not exist (%s).', $request->getParameter('id_docente')));
    $this->form = new DocenteForm($docente);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_docente'))), sprintf('Object docente does not exist (%s).', $request->getParameter('id_docente')));
    $this->form = new DocenteForm($docente);

    $this->processForm($request, $this->form);

    $this->forward($this->getModuleName(), 'edit');
  }

  public function validateUpdate()
  {
  	return true;
  }

  public function handleErrorUpdate()
  {
    return $this->forward($this->getModuleName(), 'edit');
  }

	public function executeDelete(sfWebRequest $request)
	{
		try
		{
			$request->checkCSRFProtection();
			
			$this->forward404Unless($docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_docente'))), sprintf('Object docente does not exist (%s).', $request->getParameter('id_docente')));
//			$docente->delete();
			$docente->setStDocente('E');
			$docente->save();
			
			$this->redirect('docente/index');
		}
		catch(Excepiton $e)
		{
			$this->getUser()->setFlash('erro', $e->getMessage());
			$this->redirect('docente/index');
		}
	}

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
  	try
  	{
	    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
	    if ($form->isValid())
	    {
	      $docente = $form->save();
		  $this->getUser()->setFlash('mensagem', 'Dados salvos com sucesso.');
	      $this->redirect('docente/edit?id_docente='.$docente->getIdDocente());
	    }
	    else
	    {
	    throw new Exception($form->getErrorSchema());
	    }
    }
    catch(Exception $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
	}
  }
}
